package com.mexico.inloancash.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.mexico.inloancash.bean.India_MessageInfo_ListBean;
import com.google.gson.Gson;
import com.mexico.inloancash.utils.IndiaAdjust_Helper;
import com.mexico.inloancash.utils.StatusBarUtils;
import com.mingle.widget.LoadingView;
import com.mexico.inloancash.MyApplication;
import com.mexico.inloancash.R;
import com.mexico.inloancash.bean.BaseBean;
import com.mexico.inloancash.http.IndiaRequestApi;
import com.mexico.inloancash.http.IndiaDKFactory;
import com.mexico.inloancash.utils.MyPreferencesManager;
import com.mexico.inloancash.utils.MyToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 认证首页
 */
public class IndiaHomeActivity extends BaseActivity {

    @BindView(R.id.logon_back)
    ImageView mIndiaLogonBack;


    @BindView(R.id.identitas_image)
    ImageView mIndiaIdentitasImage;

    @BindView(R.id.identitas_content)
    TextView mIndiaIdentitasContent;

    @BindView(R.id.identitas_text)
    TextView mIndiaIdentitasText;

    @BindView(R.id.identitas_sure_image)
    ImageView mIndiaIdentitas_SureImage;

    @BindView(R.id.identitas_linear)
    LinearLayout mIndiaIdentitas_Linear;

    @BindView(R.id.pribadi_image)
    ImageView mIndiaPribadi_Image;

    @BindView(R.id.pribadi_content)
    TextView mIndiaPribadi_Content;

    @BindView(R.id.pribadi_text)
    TextView mIndiaPribadi_Text;

    @BindView(R.id.pribadi_sure_image)
    ImageView mIndiaPribadi_SureImage;

    @BindView(R.id.pribadi_linear)
    LinearLayout mIndiaPribadi_Linear;

    @BindView(R.id.darurat_image)
    ImageView mIndiaDarurat_Image;

    @BindView(R.id.darurat_content)
    TextView mIndiaDarurat_Content;

    @BindView(R.id.darurat_text)
    TextView mIndiaDarurat_Text;

    @BindView(R.id.darurat_sure_image)
    ImageView mIndiaDarurat_SureImage;

    @BindView(R.id.darurat_linear)
    LinearLayout mIndiaDarurat_Linear;

    @BindView(R.id.pekerjaan_image)
    ImageView mIndiaPekerjaan_Image;

    @BindView(R.id.pekerjaan_content)
    TextView mIndiaPeker_jaan_Content;

    @BindView(R.id.pekerjaan_text)
    TextView mIndiaPeker_jaanText;

    @BindView(R.id.pekerjaan_sure_image)
    ImageView mIndiaPeker_jaanSure_Image;

    @BindView(R.id.commit_button)
    Button mIndiaCommitButton;

    @BindView(R.id.loadView)
    LoadingView loadView;

    @BindView(R.id.data_linear)
    LinearLayout mIndiaDataLinear;

    @BindView(R.id.tvAuthFace)
    ImageView mIndiaTvAuthFace;

    @BindView(R.id.tvAuthPersonalInfo)
    ImageView mIndiaTvAuthPersonalInfo;

    @BindView(R.id.tvAuthContact)
    ImageView mIndiaTvAuthContact;


    private boolean mIndiaIsEdit1 = false;//是否认证第一步
    private boolean mIndiaIsEdit2 = false;//是否认证第二步
    private boolean mIndiaIsEdit3 = false;//是否认证第三步
//    private boolean mIndiaIsEdit4 = false;//是否认证第四步
    private boolean mIndiaIsEdit5 = false;//判断工作是否认证

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.india_activity_view_authentication);
        ButterKnife.bind(this);
        StatusBarUtils.setColor(this, R.color.theme);
        if (MainActivity.isShow) {
            mIndiaCommitButton.setVisibility(View.GONE);
        } else {
            mIndiaCommitButton.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mIndiaGetAuth_Info();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.logon_back, R.id.identitas_linear, R.id.pribadi_linear, R.id.darurat_linear, R.id.commit_button})
    public void onViewClicked(View view) {
        Intent auth = new Intent(this, IndiaCerActivity.class);
        switch (view.getId()) {
            case R.id.logon_back:
                finish();
                break;
            case R.id.identitas_linear:
                if (!mIndiaIsEdit1) {
                    auth.putExtra("index", 1);
                    startActivity(auth);
                } else {
                    MyToast.show(this, "Certification");
                }
            case R.id.pribadi_linear:
                if (!mIndiaIsEdit2) {
                    if (mIndiaIsEdit1) {
                        auth.putExtra("index", 2);
                        startActivity(auth);
                    } else {
                        Toast.makeText(this,"Please complete the above certification",Toast.LENGTH_SHORT).show();
                    }
                } else if(!mIndiaIsEdit3){
                    if (mIndiaIsEdit1) {
                        auth.putExtra("index", 3);
                        startActivity(auth);
                    } else {
                        Toast.makeText(this,"Please complete the above certification",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    MyToast.show(this, "Certification");
                }

//                auth.putExtra("index", 2);
//                startActivity(auth);
                break;
            case R.id.darurat_linear:
                Intent bank = new Intent(this, IndiaAddBankActivity.class);
                startActivity(bank);
                break;

            case R.id.commit_button:
                if (mIndiaCommitButton.isSelected()) {
                    mIndiaGetLoan_Count();
                } else {
                    Toast.makeText(this,"Please complete the above certification",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    public void mIndiaGetAuth_Info() {
        Log.d("loan", "getAuthInfo");
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> info = api.getMessageInfo(MyPreferencesManager.getString("token", ""), MyApplication.getInstance().source);
        info.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try {
                    if (response.isSuccessful()) {
                        India_MessageInfo_ListBean bean = new Gson().fromJson(response.body(), India_MessageInfo_ListBean.class);
                        Log.d("loan", "onResponse:" + response.body());
                        if (bean.getCode() == 200) {
                            if (null != bean.getData()) {
                                //人脸识别
                                if (bean.getData().getOcr() == 1) {
                                    mIndiaIsEdit1 = true;
                                    mIndiaTvAuthFace.setImageResource(R.mipmap.choose_complet);
                                } else {
                                    mIndiaTvAuthFace.setImageResource(R.mipmap.choose_unfocus);
                                    mIndiaIsEdit1 = false;
                                }

                                //基本信息
                                if (bean.getData().getCominfo() == 1 && bean.getData().getContacts() == 1) {
                                    mIndiaTvAuthPersonalInfo.setImageResource(R.mipmap.choose_complet);
                                } else {
                                    mIndiaTvAuthPersonalInfo.setImageResource(R.mipmap.choose_unfocus);
                                }

                                if(bean.getData().getCominfo() == 1){
                                    mIndiaIsEdit2 = true;
                                }else {
                                    mIndiaIsEdit2 = false;
                                }
                                if(bean.getData().getContacts() == 1){
                                    mIndiaIsEdit3 = true;
                                }else {
                                    mIndiaIsEdit3 = false;
                                }

//                                //联系人
//                                if () {
//                                    authProgress++;
//                                    mIndiaDarurat_Text.setVisibility(View.GONE);
//                                    mIndiaDarurat_SureImage.setVisibility(View.VISIBLE);
//
//                                    mIndiaIsEdit3 = true;
//                                } else {
//                                    mIndiaDarurat_Text.setVisibility(View.VISIBLE);
//                                    mIndiaDarurat_SureImage.setVisibility(View.GONE);
//                                    mIndiaIsEdit3 = false;
//                                }

//                                //工作信息
//                                if (bean.getData().getWork() == 1) {
//                                    authProgress++;
//                                    mIndiaPeker_jaanText.setVisibility(View.GONE);
//                                    mIndiaPeker_jaanSure_Image.setVisibility(View.VISIBLE);
//
//                                    mIndiaIsEdit4 = true;
//                                } else {
//                                    mIndiaPeker_jaanText.setVisibility(View.VISIBLE);
//                                    mIndiaPeker_jaanSure_Image.setVisibility(View.GONE);
//                                    mIndiaIsEdit4 = false;
//                                }
                                if (bean.getData().getBankinfo() == 1) {
                                    mIndiaIsEdit5 = true;
                                    mIndiaTvAuthContact.setImageResource(R.mipmap.choose_complet);
                                } else {
                                    mIndiaIsEdit5 = false;
                                    mIndiaTvAuthContact.setImageResource(R.mipmap.choose_unfocus);
                                }
                            } else {
                                mIndiaIsEdit5 = true;
                            }
                            if (mIndiaIsEdit1 && mIndiaIsEdit2 && mIndiaIsEdit5) {
                                mIndiaCommitButton.setSelected(true);
                            } else {
                                mIndiaCommitButton.setSelected(false);
                            }
                        } else if (bean.getCode() == 401) {
                            Intent intent = new Intent(IndiaHomeActivity.this, IndiaLoginActivity.class);
                            startActivity(intent);
                        } else if (bean.getCode() == 60) {
                        } else if (bean.getCode() == 40) {
                        } else if (bean.getCode() == 41) {
                        } else if (bean.getCode() == 42) {
                        } else if (bean.getCode() == 43) {
                        } else if (bean.getCode() == 44) {
                        } else if (bean.getCode() == 45) {
                        } else if (bean.getCode() == 56) {
                        }
                    }
                } catch (Exception e) {

                }
                loadView.setVisibility(View.GONE);
                mIndiaDataLinear.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loadView.setVisibility(View.GONE);
                mIndiaDataLinear.setVisibility(View.VISIBLE);
            }
        });
    }

    public void mIndiaGetLoan_Count() {
        loading.show();
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> getLoan = api.getLoanCount(MyPreferencesManager.getString("token", ""), MyApplication.source);
        getLoan.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try {
                    if (response.isSuccessful()) {
                        BaseBean baseBean = new Gson().fromJson(response.body(), BaseBean.class);
                        if (baseBean.getCode() == 200) {

                            //统计开始申请贷款
                            IndiaAdjust_Helper.startApplicationLoan();

                            Intent success = new Intent(IndiaHomeActivity.this, IndiaAuthActivity.class);
                            startActivity(success);
                            finish();

                        } else if (baseBean.getCode() == 401) {
                            Intent intent = new Intent(IndiaHomeActivity.this, IndiaLoginActivity.class);
                            startActivity(intent);
                        } else {
                            MyToast.show(IndiaHomeActivity.this, baseBean.getMsg());
                        }
                    }
                } catch (Exception e) {

                }
                loading.dismiss();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loading.dismiss();
            }
        });
    }
}
